package com.example.medical.utils.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

   @Autowired
   private CheckUserJwtUtils checkUserJwtUtils;
   @Autowired
   private CheckLeadershipJwtUtils checkLeadershipJwtUtils;
   @Autowired
   private CheckAdminJwtUtils checkAdminJwtUtils;

    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(checkUserJwtUtils).addPathPatterns("/user/**").excludePathPatterns("/user/user/user_login").excludePathPatterns("/user/user/save_user");
        registry.addInterceptor(checkLeadershipJwtUtils).addPathPatterns("/leadership/**").excludePathPatterns("/leadership/leadership/leadership_login").excludePathPatterns("/leadership/leadership/save_leadership");
        registry.addInterceptor(checkAdminJwtUtils).addPathPatterns("/admin/**").excludePathPatterns("/admin/admin/admin_login");
    }

    /**
     * springboot 2.0配置WebMvcConfigurationSupport之后，会导致默认配置被覆盖，要访问静态资源需要重写addResourceHandlers方法
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/admin/")
                .addResourceLocations("classpath:/img/")
                .addResourceLocations("classpath:/front/")
                .addResourceLocations("classpath:/public/");
        super.addResourceHandlers(registry);
    }

}
